import '../utils/date_utils.dart';
import 'paging_model.dart';
import 'result_model.dart';

/// 学校-查询-请求
class SchoolQueryReq extends PagingQueryReq {
  final String tenantId, name;
  final List<int> stages;
  final int status;

  SchoolQueryReq({
    required this.tenantId,
    required this.name,
    required this.stages,
    required this.status,
    super.index,
    super.size,
  });

  @override
  Map<String, dynamic> toMap() {
    final map = super.toMap();
    map['name'] = name;
    map['stages'] = stages;
    map['status'] = status;
    return map;
  }
}

/// 学校-查询-响应
class SchoolQueryRes extends PagingQueryRes<SchoolItemRes> {
  SchoolQueryRes({
    required super.retCode,
    required super.retMessage,
    super.total,
    super.rows,
  });

  factory SchoolQueryRes.fromModel(final ResultModelRes res) {
    final ret = PagingQueryRes.fromModel(
      res,
      (row) => SchoolItemRes.fromMap(row),
    );
    return SchoolQueryRes(
      retCode: ret.retCode,
      retMessage: ret.retMessage,
      total: ret.total,
      rows: ret.rows,
    );
  }
}

/// 学校-数据项-响应
class SchoolItemRes {
  final String id, name, remark;
  final List<int> stages;
  final int status;
  final DateTime? createdAt;

  const SchoolItemRes({
    required this.id,
    required this.name,
    required this.remark,
    required this.stages,
    required this.status,
    this.createdAt,
  });

  factory SchoolItemRes.fromMap(final Map<String, dynamic> map) {
    final stages = <int>[];
    final items = map['stages'];
    if ((items is List) && items.isNotEmpty) {
      for (var item in items) {
        if (item is int) {
          stages.add(item);
        }
      }
    }

    return SchoolItemRes(
      id: map['id'] ?? '',
      name: map['name'] ?? '',
      remark: map['remark'] ?? '',
      stages: stages,
      status: map['status'] ?? 0,
      createdAt: dateUtils.parse('yyyy-MM-dd HH:mm:ss', map['createdAt']),
    );
  }
}

class SchoolInfoRes extends SchoolItemRes with ModelRes {
  @override
  final int retCode;
  @override
  final String retMessage;

  SchoolInfoRes({
    required this.retCode,
    required this.retMessage,
    required SchoolItemRes item,
  }) : super(
          id: item.id,
          name: item.name,
          remark: item.remark,
          stages: item.stages,
          status: item.status,
          createdAt: item.createdAt,
        );

  factory SchoolInfoRes.fromModel(final ResultModelRes res) => SchoolInfoRes(
        retCode: res.retCode,
        retMessage: res.retMessage,
        item: SchoolItemRes.fromMap(res.toMap),
      );
}

class SchoolAddReq {
  final String tenantId, name, remark;
  final List<int> stages;

  const SchoolAddReq({
    required this.tenantId,
    required this.name,
    required this.remark,
    required this.stages,
  });

  Map<String, dynamic> toMap() => {
        'name': name,
        'remark': remark,
        'stages': stages,
      };
}

class SchoolModifyReq {
  final String id, name, remark;
  final List<int> stages;
  final int status;

  const SchoolModifyReq({
    required this.id,
    required this.name,
    required this.remark,
    required this.stages,
    required this.status,
  });

  Map<String, dynamic> toMap() => {
        'name': name,
        'remark': remark,
        'stages': stages,
        'status': status,
      };
}
